package logic.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import logic.blocks.Block;
import logic.blocks.EndBlock;
import logic.blocks.Ice;
import logic.blocks.Jumper;
import logic.blocks.Lava;
import logic.blocks.Sand;
import logic.blocks.Thorns;
import logic.blocks.Wall;
import logic.characters.Player;
import logic.enemies.Enemy;
import logic.enemies.Goomba;
import logic.enemies.Paratroopa;
import logic.enemies.Soldier;
import logic.pruebas.GamePanel;

/**
 * @author Unai
 * Clase que construye el nivel en funcion de un txt
 *
 */
public class MapLoader {
	
	/**
	 * @param blocks
	 * @param enemies
	 * @param panel
	 * @param player
	 * @param difficulty
	 * @param level
	 */
	public MapLoader(ArrayList<Block> blocks, ArrayList<Enemy> enemies, GamePanel panel, Player player, int difficulty, int level, Game game) {
		try{
			FileInputStream map = new FileInputStream(new File("maps/map" + level + ".txt"));
			DataInputStream mapInput = new DataInputStream(map);
			BufferedReader mapReader = new BufferedReader(new InputStreamReader(mapInput));
			String mapLine;
			int x = 0;
			int y = 0;
			
			while ((mapLine = mapReader.readLine()) != null)   {
				char [] characters = new char [mapLine.length()];
				mapLine.getChars(0, mapLine.length(), characters, 0);
				for (int i = 0; i < mapLine.length(); i++) {
					switch (characters [i]) {
					case 'W':
						blocks.add(new Wall(x, y, panel, "resources/block.png", player));
						x = x + 50;
						break;
					case 'G':
						enemies.add(new Goomba(x, y, panel, "resources/goombaL.gif", difficulty, enemies, blocks, player, game));
						x = x + 50;
						break;
					case 'P':
						enemies.add(new Paratroopa(x, y, panel, "resources/paraLeft.gif", difficulty, enemies, blocks, player, game));
						x = x + 50;
						break; 
					case '.':
						x = x + 50;
						break;
					case 'N':
						y = y + 50;
						x = 0;
						break;
					case '8':
						blocks.add(new Wall(x, y, panel, "resources/8Block.png", player));
						x = x + 50;
						break;
					case '4':
						blocks.add(new Wall(x, y, panel, "resources/4Block.png", player));
						x = x + 50;
						break;
					case 'L':
						blocks.add(new Lava(x, y + 10, panel, "resources/lava.gif", player));
						x = x + 50;
						break;
					case 'S':
						blocks.add(new Sand(x, y, panel, "resources/sandBlock.png", player));
						x = x + 50;
						break;
					case 'I':
						blocks.add(new Ice(x, y, panel, "resources/iceBlock.png", player));
						x = x + 50;
						break;
					case 'T':
						blocks.add(new Thorns(x, y + 10, panel, "resources/thorns.png", player));
						x = x + 50;
						break;
					case 'J':
						blocks.add(new Jumper(x, y, panel, "resources/jumper.gif", player));
						x = x + 50;
						break;
					case 'O':
						blocks.add(new Ice(x, y, panel, "resources/iceBlock2.png", player));
						x = x + 50;
						break;
					case 'U':
						blocks.add(new Ice(x, y, panel, "resources/iceBlock4.png", player));
						x = x + 50;
						break;
					case 'D':
						blocks.add(new Sand(x, y, panel, "resources/sandBlock2.png", player));
						x = x + 50;
						break;
					case 'F':
						blocks.add(new Sand(x, y, panel, "resources/sandBlock4.png", player));
						x = x + 50;
						break;
					case 'R':
						enemies.add(new Soldier(x, y, panel, "resources/soldierSL.gif", difficulty, enemies, blocks, player, game));
						x = x + 50;
						break;
					case 'C':
						blocks.add(new Wall(x, y, panel, "resources/invisible20.png", player));
						x = x + 50;
						break;
					case 'V':
						blocks.add(new Wall(x, y, panel, "resources/block10v.png", player));
						x = x + 50;
						break;
					case 'E':
						blocks.add(new EndBlock(x, y, panel, "resources/end.gif", player));
						x = x + 50;
						break;
					}
				}
			}
			mapInput.close();
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
	}

}
